package m.member.order.action;

import m.common.action.ActionMeta;
import m.common.action.VuePageResult;
import m.common.action.VueResult;
import m.common.model.util.ModelQueryList;
import m.common.model.util.QueryCondition;
import m.html.VueListResult;
import m.html.element.attr.Align;
import m.html.element.attr.ButtonType;
import m.html.list.*;
import m.member.manage.model.ServeType;
import m.member.order.model.OrderDetail;
import m.member.order.service.OrderDetailService;
import m.system.exception.MException;
import m.system.util.DateUtil;
import m.system.util.JSONMessage;
import m.system.util.ObjectUtil;
import manage.action.ManageAction;

import java.util.Date;

@ActionMeta(name = "member/order/orderDetail")
public class OrderDetailAction extends ManageAction {
    private OrderDetail model;
    public JSONMessage doSave() throws Exception {
        verifyAdminOperPower("order_input_power");
        String msg=getService(OrderDetailService.class).save(model);
        return new JSONMessage(0)
                .push("msg",msg);
    }
    public JSONMessage doDelete() throws Exception {
        verifyAdminOperPower("order_input_power");
        getService(OrderDetailService.class).delete(model);
        return new JSONMessage(0)
                .push("msg","删除成功");
    }
    public VueResult toEdit() throws Exception {
        verifyAdminOperPower("order_input_power");
        if(!ObjectUtil.isEmptyOid(model)){
            model= ModelQueryList.getModel(model,"*","serve.*","serve.type.*");
            model.getServe().setPrice(model.getPrice());
            model.getServe().setDiscount(model.getDiscount());
        }else if(null==model||ObjectUtil.isEmptyOid(model.getOrder())){
            throw new MException(this.getClass(),"参数错误");
        }else{
            model.setNum(1d);
        }
        return new VuePageResult("page/member/order/orderDetail.js")
                .addParam("model", model);
    }
    /*
     * 订单明细
     */
    public VueResult subList() throws Exception {
        String oper=getParamsValue("oper");
        VueListResult result=getVueListResult("订单明细", OrderDetail.class,actionUrl("subList"));
        result.addCol(toIndexColumn());
        result.addQueryFields("serve.type.color","serve.type.name");
        result.addCol(toTextColumn("类型", "serve.type.oid", 100, true)
            .addElement(new ListElement("a-tag")
                .addAttr(":color","record.serve.type.color")
                .addText("{{record.serve.type.name}}")
            )
            .setDictType(ServeType.class)
            .setAlign(Align.CENTER)
        );
        result.addCol(toTextColumn("标题", "serve.title", 120, true));
        result.addCol(toTextColumn("描述", "serve.description", 200, true));
        result.addCol(toNumberColumn("单价", "price", 100, true));
        result.addCol(toNumberColumn("数量", "num", 100, true));
        result.addCol(toIntegerColumn("优惠","discount",100,true).setCount(ListCount.NONE)
            .addElement(ListElement.test("record.discount",
                new ListElement("a-tag")
                    .addAttr("color","processing")
                    .addText("{{record.discount}}%")
            )).setAlign(Align.CENTER)
        );
        result.addCol(toNumberColumn("金额", "total", 100, true));
        if("edit".equals(oper)) {
            result.addCol(toSpaceColumn("操作", 130,
                ListButton.modal("修改", ButtonType.PRIMARY, "action/member/order/orderDetail/toEdit", 800, SuccessMethod.REFRESH)
                    .addParam("model.oid", "oid"),
                ListButton.ajax("删除", ButtonType.ERROR, "action/member/order/orderDetail/doDelete", SuccessMethod.REFRESH)
                    .addParam("model.oid", "oid").setConfirm("确定要删除吗？")
            ).setFixed(Align.RIGHT));
            result.addButton(
                ListButton.modal("新增", ButtonType.PRIMARY, "action/member/order/orderDetail/toEdit", 800, SuccessMethod.REFRESH)
                    .addQueryParam("model.order.oid", "order.oid")
            );
        }
        result.addCondition(QueryCondition.eq("order.oid",getParamsValue("order.oid")));
        result.addSearch(ListSearch.custom("所属订单","order.oid", SearchType.HIDDEN_EQ));
        result.addSearch(ListSearch.custom("操作","oper", SearchType.HIDDEN_EQ));
        //排序 result.addOrder
        //查询条件 result.addCondition
        return result;
    }

    public OrderDetail getModel() {
        return model;
    }

    public void setModel(OrderDetail model) {
        this.model = model;
    }
}
